#include "led.h"


void led_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	//打开端口E的硬件时钟，就是对端口E供电
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
	
	//打开端口F的硬件时钟，就是对端口F供电
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
	
	//配置GPIOF的第9 10根
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9|GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
	
	//初始化
	GPIO_Init(GPIOF,&GPIO_InitStructure);
	
	//配置GPIOE的第13 14根
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13|GPIO_Pin_14;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

	//初始化
	GPIO_Init(GPIOE,&GPIO_InitStructure);	
	
	// 先全部关闭
	PFout(9)=PFout(10)=1;
	PEout(13)=PEout(14)=1;	
}


void tim14_init(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	
	//打开定时器的硬件时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);

	
	//配置定时器的参数：分频值、重载值(决定了PWM输出的频率)
	//中断的频率 演变为 PWM输出的频率
	TIM_TimeBaseStructure.TIM_Period = 10000/100-1; //PWM输出脉冲的频率为100Hz
	TIM_TimeBaseStructure.TIM_Prescaler = 8400-1; //预分频值为8400
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);
	
	
	//配置脉冲输出的引脚
	GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_TIM14);
	
	
	/* 配置TIM14 PWM的工作模式 */
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//PWM 模式 1——只要 TIMx_CNT < TIMx_CCR1，通道 1 便为有效状态，否则为无效状态。
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//打开输出PWM的通道，理解为水龙头出水。
	TIM_OCInitStructure.TIM_Pulse = 95; //当前填写比较值为50,决定了占空比
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//高电平
	
	//初始化TIM14的通道1
	TIM_OC1Init(TIM14, &TIM_OCInitStructure);
	
	
	
	//使能定时器工作
	TIM_Cmd(TIM14, ENABLE);
}

/* LED3灯光亮度设置(0~100，越小越亮) */
void set_led3_luminance(uint8_t luminance)
{
	uint8_t t;
	
	tim14_init();
	
	t = luminance;
	if(t>100)
		t = 100;
	TIM_SetCompare1(TIM14,t);
}
